import http from '@ohos.net.http';
import prompt from '@system.prompt';
import router from '@system.router';
import featureAbility from '@ohos.ability.featureAbility';
import data_storage from '@ohos.data.storage';

let storage;
export default {
    data: {
        title: 'World',
        grade: 2,
        progressPercent: 20,
        progressColor: "#00ff00",
        bgColor: "#669966",
        percent: 90,
        //协调性等级
        kneading:1,
        //灵活性等级
        stretch:1,
        //稳定性等级
        fix:2,
        //频率
        frequency:6.5625,
        //幅度
        mean_jitter:0.0002,
        //捏合次数
        FT_num:15,
        //角度
        tangle:43,
        returnContent: '',
        isRefreshing: false,
        //折线图1
        lineops1:{
            xAxis:{
                min:0,
                max:65,
                diisplay:false
            },
            yAxis:{
                min:0,
                max:0.25,
                display:false
            },
            series:{
                lineStyle:{
                    width:"2px",
                    smooth:false
                },
                topPoint:{
                    shape:"circle",
                    size:10,
                    fillColor:"#ffffff",
                    strokeColor:"#007aff"
                }
            }
        },
        lineData1:[
            {
                fillColor: "#ffb3d6fc",
                strokeColor:"#2B7CFF",
                gradient:true,
                data:[
                    0.10408552859446563,
                    0.11633875358989121,
                    0.12889670498153905,
                    0.1415723818793635,
                    0.15410066383148374,
                    0.16620776513441796,
                    0.17769845752507527,
                    0.1884188288423,
                    0.19816683333446597,
                    0.20674585627380268,
                    0.2140051596242808,
                    0.2197968888436092,
                    0.22399999664422687,
                    0.22654739977603253,
                    0.22742450277150592,
                    0.22665751271085563,
                    0.2243199482112068,
                    0.2205186801611758,
                    0.2153932952235561,
                    0.20911569238608924,
                    0.20188755373594147,
                    0.1939265372237001,
                    0.1854191713366811,
                    0.17651819552785128,
                    0.16735005671055758,
                    0.15805366554566222,
                    0.1488454180152457,
                    0.14005172294566495,
                    0.13204569580824088,
                    0.1251858102893454,
                    0.11977571494856812,
                    0.11594593214498938,
                    0.11363589393700682,
                    0.11270275796566596,
                    0.1129997614954804,
                    0.11441657277001692,
                    0.11686633461757653,
                    0.12022127077647202,
                    0.12429181833259317,
                    0.12885952294563974,
                    0.13369559275962686,
                    0.13857940613811928,
                    0.14334268033864853,
                    0.14788544626311986,
                    0.15210602555221872,
                    0.15581755524492064,
                    0.15879299142257366,
                    0.16087966561676334,
                    0.16196768059375394,
                    0.16195740318540355,
                    0.16080894196984002,
                    0.15852308603232354,
                    0.15511726280678692,
                    0.1506283003902708,
                    0.14512353696434915,
                    0.1386918890327985,
                    0.13145360370271536,
                    0.12355501294504063,
                    0.11516153190136133,
                    0.10645145185066715,
                    0.09757722474283521,
                    0.0886784235157771,
                    0.07988109249957064,
                    0.07130783955325525
                ]
            }
        ],
        test:0.0004,
        //折线图2
        lineops2:{
            xAxis:{
                min:0,
                max:100,
                diisplay:false
            },
            yAxis:{
                min:0.0000,
                max:0.0007,
                display:false
            },
            series:{
                lineStyle:{
                    width:"2px",
                    smooth:false
                }
            }
        },
        lineData2:[
            {
                fillColor: "#ffb3d6fc",
                strokeColor:"#2B7CFF",
                gradient:true,
                data:[
                    0.0004226875352970616,
                    0.00034641497048504277,
                    0.0002171934646661988,
                    0.00007183311112335389,
                    0.000050820062402756605,
                    0.00012232429849978743,
                    0.00013276146876117778,
                    0.00009177665019377557,
                    0.00009177665019377557,
                    0.000047829446528702135,
                    0.00009899827176784726,
                    0.00012181315213410926,
                    0.0001181073967462254,
                    0.00009563881743135384,
                    0.00006272837970389133,
                    0.000025507192760620967,
                    0.000012078030126261297,
                    0.000047234310433186266,
                    0.00007751930632737825,
                    0.0001001877511490612,
                    0.00011152247948986728,
                    0.00010696715012192826,
                    0.00008279427388968585,
                    0.000038435695313143725,
                    0.00002225973478564569,
                    0.00009152614368845846,
                    0.00015923610395418564,
                    0.00021457647328970708,
                    0.0002476295170485976,
                    0.0002512915788299708,
                    0.00022366016097530303,
                    0.00016998938784773543,
                    0.00010281381716925608,
                    0.00003947836384154968,
                    0.0000025762341375745734,
                    0.0000115026959868203,
                    0.000014155067444843183,
                    0.00006359115462606368,
                    0.00011631662826205869,
                    0.00014915861660247387,
                    0.00014526051302832244,
                    0.00010101660590056127,
                    0.00002765626845224745,
                    0.00005296390919040661,
                    0.00011606346998593864,
                    0.00014198788729480327,
                    0.00012241468246410037,
                    0.00006375627400512229,
                    0.000012932759324346677,
                    0.00007651111608675265,
                    0.00009512480151130376,
                    0.00004859869239037806,
                    0.00006071323662615313,
                    0.0002044264186094047,
                    0.0003355825553157388,
                    0.0004053509613211048,
                    0.0003819975623651217,
                    0.0002635537213300232,
                    0.00007877864009442348,
                    0.00012325465074436437,
                    0.0002898190827913538,
                    0.0003805324637227159,
                    0.00037710551204428455,
                    0.0002847294912364795,
                    0.0001269636455156764,
                    0.00006176661960122232,
                    0.00024278466877758476,
                    0.0003785166022987063,
                    0.0004382467427812564,
                    0.00040513982073342447,
                    0.00028262885274221716,
                    0.00009622845911083644,
                    0.00011207205532315356,
                    0.0002956308158648113,
                    0.0004162666130156367,
                    0.00045346158995318575,
                    0.00040718371060277647,
                    0.0002947841864053835,
                    0.00014397221303941166,
                    0.00001544703953060278,
                    0.00015823321356567277,
                    0.00026682004932476744,
                    0.0003307471491931224,
                    0.0003442199429564141,
                    0.0003045833946382359,
                    0.00021344963325242716,
                    0.00007951109634518616,
                    0.00007991229747617012,
                    0.00024071620692467377,
                    0.00037655117799895166,
                    0.0004636867409564261,
                    0.000484801307230498,
                    0.0004318306013528783,
                    0.0003080129707551116,
                    0.00012865962395469666,
                    0.00007973232110369821,
                    0.00028231164195169356,
                    0.0004413639565390426,
                    0.0005246820785398398
                ]
            },
        ],

        lineData: [
            {
                strokeColor: '#0081ff',
                fillColor: '#cce5ff',
                data: [763, 550, 551, 554, 731, 654, 525, 696, 595, 628, 433, 431, 754, 643, 743, 654, 526, 654],
                gradient: true,
            }
        ],
        lineOps: {
            xAxis: {
                min: 0,
                max: 20,
                display: false,
            },
            yAxis: {
                min: 0,
                max: 1000,
                display: false,
            },
            series: {
                lineStyle: {
                    width: "2px",
                    smooth: true,
                },
                headPoint: {
                    shape: "circle",
                    size: 5,
                    strokeWidth: 5,
                    fillColor: '#ffffff',
                    strokeColor: '#007aff',
                    display: true,
                },
                loop: {
                    margin: 2,
                    gradient: true,
                }
            }
        },
    },

    onInit(){
        this.refrefresh_fun();
        // if(this.progressPercent>0 && this.progressPercent<=20){
        //     this.progressColor = "#b32ee72e"
        // } else if (this.progressPercent>20 && this.progressPercent<=40){
        //     this.progressColor = "#80c8ff00"
        // } else if (this.progressPercent>40 && this.progressPercent<=60){
        //     this.progressColor = "#80ffd500"
        // } else if (this.progressPercent>60 && this.progressPercent<=80){
        //     this.progressColor = "#80ff7700"
        // } else if (this.progressPercent>80){
        //     this.progressColor = "#80ff0000"
        // }
        // if(router.getParams() == undefined){
        //     console.info("页面传参为空，本页面值采取默认值");
        //     return;
        // }else{
        //     let winfo = router.getParams().winfo;
        //     this.kneading = router.getParams().vals13;
        //     this.stretch = router.getParams().vals26;
        //     this.fix = router.getParams().vals34;
        //     this.FT_num = router.getParams().vals12;
        //     this.tangle = router.getParams().vals25;
        //     this.frequency = router.getParams().vals32;
        //     this.mean_jitter = router.getParams().vals33;
        //     this.lineData1[0].data = router.getParams().lindData1;
        //     this.lineData2[0].data = router.getParams().lindData2;
        //     this.lineops1.yAxis.min = Math.min(...this.lineData1[0].data);
        //     this.lineops1.yAxis.max = Math.max(...this.lineData1[0].data);
        //     this.lineops2.yAxis.min = Math.min(...this.lineData2[0].data);
        //     this.lineops2.yAxis.max = Math.max(...this.lineData2[0].data);
        //     console.info("传输情况-------" + winfo);
        //     console.info("频率："+this.frequency+"\n"+"幅度:"+this.mean_jitter);
        // }
    },

    onShow(){
        this.lineDraw();
    },

    launch4() {
        router.push ({
            uri: 'pages/niam/niam',
        });
    },
    launch5() {
        router.push ({
            uri: 'pages/action2/action2',
        });
    },
    launch6() {
        router.push ({
            uri: 'pages/pinggu/pinggu',
        });
    },

    refreshButton(){
        let path;
        let context = featureAbility.getContext();
        context.getFilesDir().then((filePath) => {
            path = filePath;
            storage = data_storage.getStorageSync(path + '/mystore');
            let data = storage.getSync('winfo',0);
            this.kneading = data;
            console.info("传输情况:---------" + data);
            storage.flushSync();
        })
        // let httpRequest = http.createHttp();
        // // 创建一个http，里面包括发起请求、中断请求、订阅/取消订阅HTTP Response Header 事件。
        // // 每一个HttpRequest对象对应一个Http请求。如需发起多个Http请求，须为每个Http请求创建对应HttpRequest对象。
        // //返回一个HttpRequest对象，里面包括request、destroy、on和off方法。
        //
        // let url = "http://49.4.115.226:8000/get_end_data?date=04-28";
        //
        // httpRequest.request(url, {
        //     // 注意请求方法：http.POST
        //     method: 'POST',
        // }, (err, data)=> {  // 判断是否请求成功
        //     if (!err) {  // 请求成功
        //         this.returnContent = JSON.parse(data.result);  //JSON.parse(字符串)——将字符串转换成json数据格式
        //         this.grade = this.returnContent.results[0].data.grade
        //         this.kneading = this.returnContent.results[0].data.Kneading
        //         this.stretch = this.returnContent.results[0].data.stretch
        //         this.tremble = this.returnContent.results[0].data.tremble
        //         this.muscle = this.returnContent.results[0].data.muscle
        //         this.last_update = this.returnContent.results[0].last_update
        //     } else {  // 请求失败
        //         prompt.showToast({
        //             message: data.result,
        //             duration: 3000,
        //         });
        //     }
        // })
    },

    Pull_Down(e) {

        this.isRefreshing = e.refreshing;

        this.refreshButton();
        setTimeout(() => {
            this.isRefreshing = false;
            prompt.showToast({
                message: "刷新成功"
            })
        }, 1500)
    },

    lineDraw(){
        let distance = this.distance_fun();
        const el =this.$refs.canvas;
        const ctx = el.getContext('2d');
        ctx.beginPath();
        ctx.strokeStyle = '#2B7CFF';
        ctx.moveTo(35, distance);
        ctx.lineTo(270, distance);
        ctx.lineWidth = 2;
        ctx.setLineDash([7,5]);
        ctx.stroke();
        ctx.font = '15px sans-serif';
        ctx.fillText("平均", 0, distance);
    },

    distance_fun() {
        //数据的最大值
        let max = Math.max(...this.lineData2[0].data);
        // console.info("幅度数据:" + this.lineData2[0].data);
        //数据的最小值
        let min = Math.min(...this.lineData2[0].data);
        console.info("幅度数据的最大值:" + max);
        console.info("幅度数据的最小值:" + min);
        //实际的屏幕像素px高度差
        let height_px = 140-20*2;
        //数据的高度差
        let height_value_range = max - min;
        //屏幕像素和数据之间的映射比率
        let radius = height_px/height_value_range;
        //平均值
        //let avg = 0.0002;
        let avg = this.mean_jitter;
        //最后需要移动的距离
        let distance = (max - avg)*radius + 20;
        return distance;
    },

    refrefresh_fun(){
        let jsondata;
        // 创建一个http请求
        let httpRequest = http.createHttp();
        // 创建一个http，里面包括发起请求、中断请求、订阅/取消订阅HTTP Response Header 事件。
        let url = "http://120.46.171.116:8082/get_home_last_data";
        httpRequest.request(url, {
            // 注意请求方法：http.POST
            method: http.POST,
        }, (err, data)=> {  // 判断是否请求成功
            if (!err) {  // 请求成功
                jsondata = JSON.parse(data.result);
                this.kneading = jsondata.results.kneadlevel;
                this.stretch = jsondata.results.stretchlevel;
                this.fix = jsondata.results.label;
                this.FT_num = jsondata.results.Kneading;
                this.tangle = jsondata.results.kneadtime;
                this.frequency = jsondata.results.frequency;
                this.mean_jitter = jsondata.results.mean_jitter;
                this.lineData1[0].data = jsondata.results.freq.smoothed_spectrum;
                this.lineData2[0].data = jsondata.results.jitter.jitter;
                this.lineops1.yAxis.min = Math.min(...this.lineData1[0].data);
                this.lineops1.yAxis.max = Math.max(...this.lineData1[0].data);
                this.lineops2.yAxis.min = Math.min(...this.lineData2[0].data);
                this.lineops2.yAxis.max = Math.max(...this.lineData2[0].data);
                console.info("刷新数据成功："+winfo)
            } else {  // 请求失败
                var winfo = JSON.parse(data.result);
                console.info("刷新数据失败："+winfo)
            }
        })
    }
}